package com.liyujie.dyeingmachine.ui.home.operate.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ItemReagentBinding;
import com.liyujie.dyeingmachine.listener.UniversalCallback;
import com.liyujie.dyeingmachine.mobile.entity.ReagentEntity;
import com.liyujie.dyeingmachine.views.NoDoubleClickListener;

import java.util.List;

public class ReagentAdapter extends RecyclerView.Adapter<ReagentAdapter.ItemViewHolder> {


    private List<ReagentEntity> reagentEntityList;
    private UniversalCallback mUniversalCallback;

    public void setReagentEntityList(List<ReagentEntity> reagentEntityList) {
        this.reagentEntityList = reagentEntityList;
        notifyDataSetChanged();
    }

    public void setUniversalCallback(UniversalCallback mUniversalCallback) {
        this.mUniversalCallback = mUniversalCallback;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemReagentBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_reagent, parent, false);
        return new ItemViewHolder(binding);
    }


    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        ReagentEntity mReagentEntity = reagentEntityList.get(position);
        if (null != mReagentEntity) {
            //试剂名称
            holder.binding.ReagentName.setText(mReagentEntity.ReagentName);
            //染色时间
            holder.binding.ReagentDyeingTime.setText(mReagentEntity.ReagentDyeingTime + "");
            //创建时间
            holder.binding.ReagentCreationTime.setText(TimeUtils.millis2String(mReagentEntity.ReagentCreationTime));
            //点击
            holder.binding.itemReagent.setOnClickListener(new NoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if (null != mUniversalCallback) {
                        mUniversalCallback.onUniversalCallback(position, mReagentEntity);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == reagentEntityList ? 0 : reagentEntityList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemReagentBinding binding;

        public ItemViewHolder(@NonNull ItemReagentBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
